package com.bjc.platform.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.security.core.context.SecurityContextHolder;

import com.bjc.platform.Constants;
import com.bjc.platform.model.AdmOrg;
import com.bjc.platform.model.CustomUserDetails;

public class StaticMethod {
	private static String timeFormat = "yyyy-MM-dd";
	private static String codeTimeFormat = "yyyyMMdd";
	private static String simpleMonthFormat="yyyy-MM";
	public static int SALES_OW_ID=-1;
	public static int NORMAL_OW_ID=-1;
	
	public static String date2String(Date date) {
		DateFormat format = new SimpleDateFormat(timeFormat);
		String s = format.format(date);
		return s;
	}

	public static String date2CodeString(Date date) {
		DateFormat format = new SimpleDateFormat(codeTimeFormat);
		String s = format.format(date);
		return s;
	}

	/**
	 * @param object
	 *            该对象有可能来自于下拉列表
	 * @return
	 */
	public static int object2Int(Object object) {
		if (object == null || (object.toString().length() == 0)
				|| (object.toString().equals(Constants.SELECTED_EMPTY_VALUE))) {
			return 0;
		} else {
			return Integer.parseInt(object.toString());
		}
	}

	public static float object2Float(Object object) {
		if (object == null || (object.toString().length() == 0)) {
			return 0.00f;
		} else {
			return Float.parseFloat(object.toString());
		}
	}

	public static boolean object2Boolean(Object object) {
		if (object == null) {
			return false;
		} else {
			return Boolean.parseBoolean(object.toString());
		}
	}

	public static long object2Long(Object object) {
		if (object == null || (object.toString().length() == 0)
				|| (object.toString().equals(Constants.SELECTED_EMPTY_VALUE))) {
			return 0l;
		} else {
			return Long.parseLong(object.toString());
		}
	}

	public static double object2Double(Object object) {
		if (object == null || (object.toString().length() == 0)) {
			return 0d;
		} else {
			return Double.parseDouble(object.toString());
		}
	}

	public static String object2String(Object object) {
		if (object == null || (object.toString().length() == 0)|| (object.toString().equals(Constants.SELECTED_EMPTY_VALUE))) {
			return "";
		} else {
			return object.toString();
		}
	}

	public static Date object2Date(Object object) {
		if (object == null || String.valueOf(object).length() == 0 || "".equals(object)) {
			return null;
		} else {
			DateFormat format = new SimpleDateFormat(timeFormat);
			Date date = null;
			try {
				date = format.parse(object.toString());
			} catch (ParseException e) {
				System.out.println("解析错误");
			}
			return date;
		}
	}
	
	public static Date object2MonthDate(Object object) {
		if (object == null || String.valueOf(object).length() == 0 || "".equals(object)) {
			return null;
		} else {
			DateFormat format = new SimpleDateFormat(simpleMonthFormat);
			Date date = null;
			try {
				date = format.parse(object.toString());
			} catch (ParseException e) {
				System.out.println("解析错误");
			}
			return date;
		}
	}

	public static JSONResult TOJSONResult(boolean success, Object results) {
		JSONResult result = new JSONResult(success, results);
		return result;
	}

	public static JSONResult TOListJSONResult(boolean success, Object results,
			int size) {
		JSONResult result = new JSONResult(success, results, size);
		return result;
	}

	public static JSONResult TOListJSONResult(boolean success, Object results,
			Long size) {
		JSONResult result = new JSONResult(success, results, size);
		return result;
	}

	/**
	 * 获取当前登录信息
	 * 
	 * @return
	 */
	public static CustomUserDetails getUser() {
		try {
			CustomUserDetails user = (CustomUserDetails) SecurityContextHolder
					.getContext().getAuthentication().getPrincipal();
			return user;
		} catch (Exception e) {
			// nothing to do
		}
		return new CustomUserDetails();
	}

	/**
	 * 获取当前登录信息
	 * 
	 * @return
	 */
	public static AdmOrg getOperationCompany() {
		try {
			CustomUserDetails user = (CustomUserDetails) SecurityContextHolder
					.getContext().getAuthentication().getPrincipal();
			return user.getOperCompany();
		} catch (Exception e) {
			// nothing to do
		}
		return null;
	}
}
